﻿<UserControl x:Class="TilePicker"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:local="clr-namespace:RawExplorer2.UI"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             x:Name="uc"
             Background="DarkGray"
             SnapsToDevicePixels="True"
             d:DesignHeight="300"
             d:DesignWidth="300"
             mc:Ignorable="d">
    <UserControl.Resources>
        <SolidColorBrush x:Key="HighlightBackground" Color="#800000FF" />
        <SolidColorBrush x:Key="HighlightForeground" Color="Blue" />
        <SolidColorBrush x:Key="SelectedBackground" Color="Transparent" />
        <SolidColorBrush x:Key="SelectedForeground" Color="#FFFF00" />
        <SolidColorBrush x:Key="TransparentBrush" Color="Transparent" />
    </UserControl.Resources>
    <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
        <Grid HorizontalAlignment="Left"
              VerticalAlignment="Top"
              Background="Black">
            <Image Name="BackgroundImage"
                   RenderOptions.BitmapScalingMode="NearestNeighbor"
                   Source="{Binding Image,
                                    ElementName=uc}"
                   Stretch="None" />
            <ItemsControl Width="{Binding ElementName=BackgroundImage,
                                          Path=ActualWidth}"
                          Height="{Binding ElementName=BackgroundImage,
                                           Path=ActualHeight}"
                          ItemsSource="{Binding Path=Tiles,
                                                ElementName=uc}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid HorizontalAlignment="Stretch"
                                     VerticalAlignment="Stretch"
                                     Columns="{Binding Path=Columns,
                                                       ElementName=uc}"
                                     Rows="{Binding Path=Rows,
                                                    ElementName=uc}" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Rectangle Name="TileRectangle"
                                   HorizontalAlignment="Stretch"
                                   VerticalAlignment="Stretch"
                                   Focusable="False"
                                   MouseDown="TileRectangle_MouseDown"
                                   StrokeThickness="2">
                            <Rectangle.Style>
                                <Style TargetType="{x:Type Rectangle}">
                                    <Setter Property="Fill" Value="{StaticResource TransparentBrush}" />
                                    <Setter Property="Stroke" Value="{StaticResource TransparentBrush}" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding IsSelected}" Value="True">
                                            <Setter Property="Fill" Value="{StaticResource SelectedBackground}" />
                                            <Setter Property="Stroke" Value="{StaticResource SelectedForeground}" />
                                        </DataTrigger>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter Property="Fill" Value="{StaticResource HighlightBackground}" />
                                            <Setter Property="Stroke" Value="{StaticResource HighlightForeground}" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Rectangle.Style>
                        </Rectangle>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </ScrollViewer>
</UserControl>
